package com.anbinh.doctor.fragment.clinic.benhnhan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.FragmentMain;
import com.anbinh.doctor.fragment.clinic.FragmentDoctorList;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;

public class FragmentClinicAddPatients extends Fragment {
	private static FragmentClinicAddPatients fragment = null;
	private View mView;
	private View mLoadding;
	private UserLogin userLogin;
	private EditText edPhone;

	private EditText edName;
	private int doctor_id;

	public static FragmentClinicAddPatients newInstance() {
		if (fragment == null) {
			fragment = new FragmentClinicAddPatients();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_add_patients, null);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);
		mLoadding = (View) mView.findViewById(R.id.loadding);

		View btnContact = (View) mView.findViewById(R.id.btnContact);
		btnContact.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK,
						ContactsContract.Contacts.CONTENT_URI);
				getActivity().startActivityForResult(intent,
						Utils.SELECT_PHONE_CONTACT_ADD_PATIENT);
			}
		});

		edName = (EditText) mView.findViewById(R.id.lblName);
		edPhone = (EditText) mView.findViewById(R.id.edPhone);

		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		new CountDownTimer(500, 500) {

			@Override
			public void onTick(long millisUntilFinished) {

			}

			@Override
			public void onFinish() {
				Utils.hideKeyboard(getActivity());
			}
		}.start();

	}

	public void setPhone(String phone) {
		edPhone.setText(phone);
	}

	public void pustData() {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		String mFull_name = edName.getText().toString().trim();
		String mPhone = edPhone.getText().toString().trim();

		ServerController.getInstance(getActivity()).requestPushPatients(
				userLogin.getData().getToken(), FragmentDoctorList.newInstance().getCurrentDoctors().getId(), mFull_name, mPhone,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						if (getActivity() == null)
							return;
						Utils.showMessage(getActivity(), data.getDescription());
						if (data.getResponsecode() > 0) {
							FragmentMain.newInstance().setRefreshDoctor(true);
							Intent intent = new Intent();
							intent.putExtra(Utils.KEY, Utils.ADD_PATIENT);
							getActivity().setResult(Integer.MAX_VALUE, intent);
							getActivity().finish();
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}

				});
	}
}
